package cn.wolfcode.education.service.impl;

import cn.wolfcode.education.domain.School;
import cn.wolfcode.education.domain.Speech;
import cn.wolfcode.education.mapper.SchoolMapper;
import cn.wolfcode.education.mapper.SpeechMapper;
import cn.wolfcode.education.service.ISpeechService;
import cn.wolfcode.education.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 演讲业务
 */
@Service
public class SpeechServiceImpl implements ISpeechService {
    @Autowired
    private SpeechMapper speechMapper;
    @Autowired
    private SchoolMapper schoolMapper;

    public void insertSpeech(Speech speech) {
        //判断是否有学校
        if (speech.getSchool().getId() != null){
            //设置学校的演讲次数
            School school = schoolMapper.getSchoolById(speech.getSchool().getId());
            schoolMapper.updateSchoolSpeechTotal(school.getId(),school.getSpeechTotal() + 1);
        }
        speechMapper.insert(speech);
    }

    public PageResult getSpeechListBySchoolId(Long schoolId) {
        List<Speech> list = speechMapper.getSpeechListBySchoolId(schoolId);
       /* for(Speech s : list){
            System.out.println(s.getSchool().getId());
            System.out.println(s.getSchool().getNextTime());
        }*/
        if (list.size() == 0){
            return new PageResult();
        }
        return new PageResult(list.size(),list);
    }
}
